#include "../include/disorderArray.h"


int partition(int a[],int begin,int end ){
	int i,j;
	int key = a[end];
	i = begin -1;
	for(j = begin ; j < end ; j++ ){
		if(a[j] <=  key){
			i = i +1;
			if(i==j){
				continue;
			}
			swap(a+i,a+j);
		}
	}
	swap(a+i+1,a+end);
	return i+1;
}

int printArray(int a[],int n){
	int i;
	for(i =0;i<n;i++){
		printf("%d ",a[i]);
	}
	printf("\n");
	return 0;
}

int findMinKNumber( int a[],int n,int k){
	if( n < k){
		printf("the k is too bigger!\n");
		return -1;
	}
	int p,start,end;
	start = 0;
	end =  n  -1;
	while(1){
		p  = partition(a ,start,end);
		if( p+1 ==  k){
			printArray(a,k);
			break;
		}else if(p < k -1 ){
			start = p + 1;
		}else{
			end  = p - 1;
		}

	}
	return 0;
}
